package training;

import java.util.HashSet;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_6_21_Training {
    //钟楼管理员
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while(T>0){
            T--;
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            HashSet<Integer> set = new HashSet<>();
            dfs(n,m,set,1);
            System.out.println(set.size());
        }
    }

    public static void dfs(int n,int m,HashSet set,int pos){
        if(m==0){
            set.add(pos);
            return;
        }
        dfs(n,m-1,set,pos%n+1);
        dfs(n,m-1,set,pos==1?n:(pos-1)%n);
    }
}





